package li.webqq.util;

import java.net.URLEncoder;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author 明伟
 */
public class StringUtil {
    private static final Map<String, Integer> sequence = new ConcurrentHashMap<String, Integer>();

    /**
     * SendBuddyMsg2 SendQunMsg2 这俩的递增msgid到底是一起递增的还是分别的
     */
    public static Integer getNext() {
        String caller = Thread.currentThread().getStackTrace()[2].getClassName();
        Integer next = sequence.get(caller);
        if (null == next) {
            next = new Random().nextInt(30000000);
        }
        sequence.put(caller, next + 1);
        return next + 1;
    }

    public static String random() {
        Double num1 = Math.random();
        Double num2 = Math.random();

        String str1 = num1.toString();
        String str2 = num2.toString();

        return str1.substring(0, 10) + str2.substring(str2.length() - 10);
    }

    public static String encode(String str) {
        return URLEncoder.encode(str);
    }

    public static String build(String temp, Object... args) {
        for (int i = 0; i < args.length; i++) {
            temp = temp.replace("{=" + i + "=}", args[i] + "");
        }
        return temp;
    }

    public static Boolean equals(String str1, String str2) {
        if (null != str1) {
            return str1.equals(str2);
        } else {
            return null == str2;
        }
    }

    public static String dup(CharSequence cs, int num) {
        if (isEmpty(cs) || num <= 0)
            return "";
        StringBuilder sb = new StringBuilder(cs.length() * num);
        for (int i = 0; i < num; i++)
            sb.append(cs);
        return sb.toString();
    }

    public static boolean isEmpty(CharSequence cs) {
        return null == cs || cs.length() == 0;
    }

    /**
     * 获取好友时的HASH参数
     */
    public static String hashWhenGetUserFriends2(String uin, String ptwebqq) {
        String a = ptwebqq + "password error";
        String s = "";
        while (true) {
            if (s.length() <= a.length()) {
                s += uin;
                if (s.length() == a.length()) {
                    break;
                }
            } else {
                s = s.substring(0, a.length());
                break;
            }
        }
        int[] j = new int[s.length()];
        for (int d = 0; d < j.length; d++) {
            j[d] = s.charAt(d) ^ a.charAt(d);
        }
        String[] w = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F" };
        s = "";
        for (int d = 0; d < j.length; d++) {
            s += w[j[d] >> 4 & 15];
            s += w[j[d] & 15];
        }
        return s;
    }

    /**
     * 密码加密
     */
    public static String encodePassword(String pt_uin, String password, String verify_code) {
        String[] us = pt_uin.split("\\\\x");
        byte[] uinByte = new byte[us.length - 1];
        if (us.length == 9) {
            for (int i = 1; i < us.length; i++) {
                uinByte[i - 1] = getByte(Integer.parseInt(us[i], 16));
            }
        }
        return md5(md5(getByte(hexchar2bin(md5(password)), uinByte)) + verify_code.toUpperCase());
    }

    private static String md5(byte[] source) {
        String s = null;
        char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
        try {
            java.security.MessageDigest md = java.security.MessageDigest.getInstance("MD5");
            md.update(source);
            byte tmp[] = md.digest();
            char str[] = new char[16 * 2];
            int k = 0;
            for (int i = 0; i < 16; i++) {
                byte byte0 = tmp[i];
                str[k++] = hexDigits[byte0 >>> 4 & 0xf];
                str[k++] = hexDigits[byte0 & 0xf];
            }
            s = new String(str);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return s.toUpperCase();
    }

    private static String md5(String str) {
        return md5(str.getBytes()).toUpperCase();
    }

    private static byte[] hexchar2bin(String str) {
        byte[] bytes = new byte[str.length() / 2];
        int j = 0;
        for (int i = 0; i < str.length(); i = i + 2) {
            int iv = Integer.parseInt(str.substring(i, i + 2), 16);
            bytes[j++] = getByte(iv);
        }
        return bytes;
    }

    private static byte getByte(int intValue) {
        int byteValue = 0;
        int temp = intValue % 256;
        if (intValue < 0) {
            byteValue = temp < -128 ? 256 + temp : temp;
        } else {
            byteValue = temp > 127 ? temp - 256 : temp;
        }
        return (byte) byteValue;
    }

    private static byte[] getByte(byte[] b1, byte[] b2) {
        byte[] bs = new byte[b1.length + b2.length];
        int i = 0;
        for (i = 0; i < b1.length; i++) {
            bs[i] = b1[i];
        }
        for (int j = 0; j < b2.length; j++) {
            bs[i + j] = b2[j];
        }
        return bs;
    }
}